<template>
    <div>
        <div class="filter">
            <el-form :inline="true" :model="formInline" ref="ruleForm">
                <el-form-item label="房屋编号" prop="roomNumber">
                    <el-input v-model="formInline.roomNumber" placeholder="请输入房屋编号"></el-input>
                </el-form-item>
                <el-form-item label="房屋户型" prop="type">
                    <el-input v-model="formInline.type" placeholder="请输入房屋户型"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="onSubmit">查询</el-button>
                    <el-button @click="resetForm('ruleForm')">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
        <div class="table">
            <div class="add">
                <el-button type="primary" @click="add">新增房屋</el-button>
            </div>
            <div class="data">
                <el-table :data="tableData" border style="width: 100%">
                    <el-table-column fixed prop="roomNumber" label="房屋编号" width="150">
                    </el-table-column>
                    <el-table-column prop="size" label="大小">
                    </el-table-column>
                    <el-table-column prop="price" label="价格">
                    </el-table-column>
                    <el-table-column prop="type" label="户型">
                    </el-table-column>
                    <el-table-column prop="detail" label="详情">
                    </el-table-column>
                    <el-table-column fixed="right" label="操作" width="100">
                        <template slot-scope="scope">
                            <el-button type="text" size="small" @click="getRoomById(scope.row.id)">编辑</el-button>
                            <el-button @click="deleteRoom(scope.row.id)" type="text" size="small">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageInfo.page" :page-sizes="[10, 20]" :page-size="pageInfo.size" layout="total, sizes, prev, pager, next, jumper" :total="pageInfo.total">
            </el-pagination>
        </div>
        <el-dialog :title="isAdd?'新增房屋信息':'编辑房屋信息'" width="600px" :visible.sync="dialogFormVisible" center>
            <el-form :model="addForm" label-width="100px" :rules="addRules" ref="addForm">
                <el-form-item label="房屋图片">
                    <el-upload action="http://192.168.101.13:10001/api/images/upload" name="pic" list-type="picture-card" multiple :limit="3" :on-success="uploadImage" :file-list="fileList" :on-remove="handleRemove">
                        <i class="el-icon-plus"></i>
                    </el-upload>
                </el-form-item>
                <el-form-item label="房屋编号" prop="roomNumber">
                    <el-input v-model="addForm.roomNumber"></el-input>
                </el-form-item>
                <el-form-item label="大小" prop="size">
                    <el-input v-model="addForm.size"></el-input>
                </el-form-item>
                <el-form-item label="价格" prop="price">
                    <el-input v-model="addForm.price"></el-input>
                </el-form-item>
                <el-form-item label="户型" prop="type">
                    <el-input v-model="addForm.type"></el-input>
                </el-form-item>
                <el-form-item label="详情" prop="detail">
                    <el-input v-model="addForm.detail"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="commitForm('addForm')">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import api from "../../../api";
export default {
    roomNumber: "house",
    data() {
        return {
            formInline: {
                roomNumber: "",
                type: "",
            },
            pageInfo: {
                currentPage: 1,
                pageSize: 10,
                total: 0,
            },
            dialogFormVisible: false,
            addForm: {
                roomNumber: "",
                phone: "",
                introduce: "",
                place: "",
                price: "",
                size: "",
                images: "",
            },
            fileList: [],
            tableData: [],
            isAdd: true,
            addRules: {
                roomNumber: [
                    {
                        required: true,
                        message: "请输入房屋",
                        trigger: "blur",
                    },
                ],
                detail: [
                    {
                        required: true,
                        message: "请输入详情",
                        trigger: "blur",
                    },
                ],
                type: [
                    { required: true, message: "请输入户型", trigger: "blur" },
                ],
                size: [
                    {
                        required: true,
                        message: "请输入大小",
                        trigger: "blur",
                    },
                ],
                price: [
                    {
                        required: true,
                        message: "请输入价格",
                        trigger: "blur",
                    },
                ],
            },
        };
    },
    mounted() {
        this.getRoomList();
    },
    methods: {
        onSubmit() {
            this.pageInfo.currentPage = 1;
            this.getRoomList();
        },
        resetForm(formNumber) {
            this.$refs[formNumber].resetFields();
        },
        // 删除图片
        handleRemove(file, fileList) {
            this.fileList = fileList;
        },
        // 上传成功
        uploadImage(res, file, fileList) {
            this.fileList = fileList;
        },
        // 分页获取房屋信息数据
        async getRoomList() {
            try {
                const params = Object.assign(
                    {},
                    this.pageInfo,
                    this.formInline
                );
                const response = await api.room.getRoomPage(params);
                if (response.data.code === 200) {
                    this.tableData = response.data.data.records;
                    this.pageInfo.total = response.data.data.total;
                } else {
                    this.$message.error(response.data.msg);
                }
            } catch (error) {
                this.$message.error(error);
            }
        },
        // 根据ID获取房屋信息
        async getRoomById(id) {
            this.isAdd = false;
            this.dialogFormVisible = true;
            try {
                const response = await api.room.getRoomById({
                    roomId: id,
                });
                if (response.data.code === 200) {
                    this.addForm = response.data.data;
                    this.fileList = this.addForm.images
                        .split(",")
                        .map((item) => {
                            return {
                                name: item,
                                url: item,
                                response: { data: item },
                            };
                        });
                } else {
                    this.$message.error(response.data.msg);
                }
            } catch (error) {
                this.$message.error(error);
            }
        },
        // 添加或者编辑提交
        async commitForm(formNumber) {
            this.$refs[formNumber].validate((valid) => {
                if (valid) {
                    if (this.isAdd) {
                        this.commitAddForm();
                    } else {
                        this.commitEditForm();
                    }
                    this.dialogFormVisible = false;
                    this.addForm = this.$options.data().addForm; //重置表单数据
                }
            });
        },
        // 提交添加用户
        async commitAddForm() {
            try {
                let images = [];
                this.fileList.forEach((element) => {
                    images.push(element.response.data);
                });
                this.addForm.images = images.join(",");
                const response = await api.room.addRoom(this.addForm);
                if (response.data.code === 200) {
                    this.$message.success("添加成功");
                    this.getRoomList();
                } else {
                    this.$message.error(response.data.msg);
                }
            } catch (error) {
                this.$message.error(error);
            }
        },
        // 提交编辑用户
        async commitEditForm() {
            try {
                let images = [];
                this.fileList.forEach((element) => {
                    images.push(element.response.data);
                });
                this.addForm.images = images.join(",");
                const response = await api.room.editRoom(this.addForm);
                if (response.data.code === 200) {
                    this.$message.success("编辑成功");
                    this.getRoomList();
                } else {
                    this.$message.error(response.data.msg);
                }
            } catch (error) {
                this.$message.error(error);
            }
        },
        // 点击新增按钮
        add() {
            this.isAdd = true;
            this.dialogFormVisible = true;
            this.addForm = this.$options.data().addForm;
            this.fileList = [];
        },
        // 删除房屋信息
        deleteRoom(id) {
            this.$confirm("确认删除, 是否继续?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(async () => {
                    try {
                        const response = await api.room.deleteRoom({
                            roomId: id,
                        });
                        if (response.data.code === 200) {
                            this.$message.success("删除成功");
                            this.getRoomList();
                        } else {
                            this.$message.error(response.data.msg);
                        }
                    } catch (error) {
                        this.$message.error(error);
                    }
                })
                .catch(() => {});
        },
        handleSizeChange(val) {
            this.pageInfo.size = val;
            this.getRoomList();
        },
        handleCurrentChange(val) {
            this.pageInfo.currentPage = val;
            this.getRoomList();
        },
    },
};
</script>

<style lang='less' scoped>
</style>